package com.coder4j.utils;

import com.coder4j.starter.web.BaseException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 时间处理工具类，获取指定格式的日期字符串
 * @author Keller
 * @date 2019-05-30 08:55:13
 * TODO 这个工具类需要重构，使用 jdk 1.8 的 LocalDate 和 LocalTime 实现
 */
public class DateUtils {


    public static String convertSecondsToHMS(int seconds) {
        int hours = seconds / 3600;
        int minutes = (seconds % 3600) / 60;
        int remainingSeconds = seconds % 60;

        return String.format("%02d:%02d:%02d", hours, minutes, remainingSeconds);
    }

    public static boolean isMonth(Integer month){
        return month != null && month >= 1 && month <= 12;
    }

    public static Date localDate2Date(LocalDate localDate){
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    private static SimpleDateFormat dateFormat =
            new SimpleDateFormat("yyyy-MM-dd");

    private static SimpleDateFormat datetimeFormat =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 获取时间字符串
     * @param date
     * @return
     */
    public static String getDate(Date date){
        if(date == null){
            return null;
        }
        return dateFormat.format(date);
    }


    public static String getDate(){
        return getDate(new Date());
    }

    public static String getDate(Date date ,String pattern){
        if(date == null){
            return null;
        }
        SimpleDateFormat format =
                new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static String getDate(String pattern){
        return getDate(new Date(),pattern);
    }

    public static Date parseDate(String dateStr){
        try {
            return dateFormat.parse(dateStr);
        } catch (Exception e) {
            return parseDateTime(dateStr);
        }
    }

    /**
     * 获取日期时间字符串
     * @param date
     * @return
     */
    public static String getDateTime(Date date){
        if(date  == null){
            return null;
        }
        return datetimeFormat.format(date);
    }

    public static String getDateTime(){
        return getDateTime(new Date());
    }

    public static Date parseDateTime(String datetimeStr){
        try {
            return datetimeFormat.parse(datetimeStr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getVersion(){
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyyMMdd_HHmmss");
        return simpleDateFormat.format(new Date());
    }

    public static String getTimeMaskSecond(){
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyyMMddHHmmss");
        return simpleDateFormat.format(new Date());
    }


    /**
     * 获取时间标识，年月日时分秒毫秒，用于文件名
     * @param date
     * @return
     */
    public static String getTimeMask(Date date){
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return simpleDateFormat.format(date);
    }

    public static String getTimeMask(){
        return getTimeMask(new Date());
    }


    public static String getShortTimeMask(Date date){
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyyMMddHHmmss");
        return simpleDateFormat.format(date);
    }

    public static String getShortTimeMask(){
        return getShortTimeMask(new Date());
    }


    public static Date getDateByYearBegin(Integer year){
        //年份没传，使用当前年份
        if(year == null || year <= 0){
            year = LocalDate.now().getYear();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = year + "-01-01 00:00:00";
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            throw BaseException.error("日期转化失败");
        }
    }

    public static Date getDateByYearEnd(Integer year){
        //年份没传，使用当前年份
        if(year == null || year <= 0){
            year = LocalDate.now().getYear();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = year + "-12-31 23:59:59";
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            throw BaseException.error("日期转化失败");
        }
    }

    public static String getDurationString(Date date){
        // 设定两个时间点
        LocalDateTime time1 = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime time2 = LocalDateTime.now();
        long seconds = Duration.between(time1, time2).getSeconds();

        int minute = 60;
        int hour = 60 * minute;
        int day = 24 * hour;

        long d = seconds / day;
        long h = (seconds % day) / hour;
        long m = (seconds % hour) / minute;
        long s = seconds % minute;

        String str = "";
        if(seconds > day){
            str = String.format("%s天 %s小时 %s分 %s秒",d,h,m,s);
        }else if(seconds > hour){
            str = String.format("%s小时 %s分 %s秒",h,m,s);
        } else if (seconds > minute) {
            str = String.format("%s分 %s秒",m,s);
        }else {
            str = String.format("%s秒",s);
        }
        return str;
    }

    public static void main(String[] args){


        System.out.println(getDurationString(parseDateTime("2024-04-30 00:00:00")));
    }
}
